#!/usr/bin/perl

$conffile="config/sshservers.conf";

my @serv = ();
my @login = ();
my @passwd = ();
my @displayline = ();
if (open(my $fh, '<:encoding(UTF-8)', $conffile)) {
    while (my $row = <$fh>) {
        chomp $row;
        @tw = split(' ', $row);
        push(@serv, @tw[0]);
        push(@login, @tw[1]);
        push(@passwd, @tw[2]);
        $line="@tw[0]  @tw[1] @tw[2]";
        push(@displayline, $line);
    }
} else {
    die "File $conffile must exist.\n";
}

sub displayInfo {
    @data=@_;
    foreach $item (@data) {
        printf("\t %d\t $item\n", $i++);
    }
}

print " Which Server you want to connect to :\n\n";
&displayInfo(@displayline);

print "\nGive me the Number: ";

while(<>) {
    $n=$_;
    #$connstr="expect -c 'set timeout -1; spawn ssh -o StrictHostKeyChecking=no $login[$n]@"."$serv[$n]; expect *assword:; send -- $passwd[$n]\r; interact;'";
    #$connstr="./connectauto.sh $login[$n]@"."$serv[$n] $passwd[$n]";
    $connstr="ssh -o StrictHostKeyChecking=no $login[$n]@"."$serv[$n]; expect -c \"{ set timeout 1; expect 'password: '; send $passwd[$n]\r}\"";
    print "\n$connstr\n";
    do { system($connstr); exit;};
}

